package com.rd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rd.dto.Result;
import com.rd.dto.UserDTO;
import com.rd.entity.Follow;
import com.rd.mapper.FollowMapper;
import com.rd.service.FollowService;
import com.rd.service.UserService;
import com.rd.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

// 用户关注取关
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements FollowService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private UserService userService;


    // 关注功能
    @Override
    public Result follow(Long followUserId, Boolean isFollow) {
        Long userId = UserHolder.getUser().getId();
        String key = "follows:" + userId;

        QueryWrapper<Follow> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
                .eq("follow_user_id", followUserId);

        if (isFollow){   // 关注
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(followUserId);

            Follow follow1 = getOne(wrapper);
            if (follow1 == null){   //若没有关注该用户，才插入数据
                boolean flag = save(follow);
                if (flag){
                    stringRedisTemplate.opsForSet().add(key,followUserId.toString());
                }
            }else{
                return Result.ok("已经关注过了");
            }
        }else{      //取关
            //同等于 delete from tb_follow where user_id = ? and follow_user_id = ?

            boolean flag = remove(wrapper);// 删除数据
            if (flag){
                stringRedisTemplate.opsForSet().remove(key,followUserId.toString());
            }
        }
        return Result.ok();
    }

    // 用户是否关注
    @Override
    public Result isFollow(Long followUserId) {
        Long userId = UserHolder.getUser().getId();
        Integer count = query()
                        .eq("user_id", userId)
                        .eq("follow_user_id", followUserId)
                        .count();

        return Result.ok(count > 0);
    }

    @Override
    public Result followCommons(Long id) {

        System.out.println("followCommons");
        Long userId = UserHolder.getUser().getId();
        String key = "follows:" + userId;
        String key2 = "follows:" + id;
        // 用户的共同关注
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key, key2);
        if (intersect == null || intersect.isEmpty()){
            return Result.ok(Collections.emptyList());
        }
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        List<UserDTO> users = userService.listByIds(ids)
                .stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());

        System.out.println("共同关注："+users);
        return Result.ok(users);
    }
}
